package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.CodeConstant;
import com.zb.entity.*;
import com.zb.mapper.ZjStoreBoundLogMapper;
import com.zb.service.*;
import com.zb.utils.TransactionUtil;
import com.zb.vo.StoreBoundVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 仓库出入库日志表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-12
 */
@Service
public class ZjStoreBoundLogServiceImpl extends ServiceImpl<ZjStoreBoundLogMapper, ZjStoreBoundLog> implements ZjStoreBoundLogService {
    private static final Logger logger = LoggerFactory.getLogger(ZjStoreBoundLogServiceImpl.class);
    @Resource
    private ZjStoreBoundLogMapper storeBoundLogMapper;
    @Resource
    private ZjStoreBoundLog02Service storeBoundLog02Service;
    @Resource
    private ZjStoreBoundLog03Service storeBoundLog03Service;
    @Resource
    private ZjStoreBoundLog04Service storeBoundLog04Service;
    @Resource
    private ZjStoreBoundLog05Service storeBoundLog05Service;
    @Resource(name = "asyncServiceExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Resource
    private PlatformTransactionManager platformTransactionManager;

    // 保存出库日志
    @Override
    public void saveOutboundLog(StoreBoundVo storeBoundVo) {
        List<StoreBoundVo> storeBoundVos = new ArrayList<>();
        storeBoundVos.add(storeBoundVo);
        this.saveOutboundLog(storeBoundVos);
    }

    // 保存出库日志
    @Override
    public void saveOutboundLog(List<StoreBoundVo> storeBoundVos) {
        excuteSave(2, storeBoundVos);
    }

    // 保存入库日志
    @Override
    public void saveInboundLog(StoreBoundVo storeBoundVo) {
        List<StoreBoundVo> storeBoundVos = new ArrayList<>();
        storeBoundVos.add(storeBoundVo);
    }

    // 保存入库日志
    @Override
    public void saveInboundLog(List<StoreBoundVo> storeBoundVos) {
        excuteSave(1, storeBoundVos);
    }

    /**
     * 执行保存出入库日志
     * @param type 日志类型：1-入库，2-出库
     * @param storeBoundVos
     */
    private void excuteSave(Integer type, List<StoreBoundVo> storeBoundVos) {
        if (CollectionUtil.isEmpty(storeBoundVos)) {
            return;
        }
        threadPoolTaskExecutor.execute(() -> {
            TransactionStatus status = platformTransactionManager.getTransaction(TransactionUtil.transactionDefinition());
            List<ZjStoreBoundLog> logs = new ArrayList<>();
            List<ZjStoreBoundLog02> log02s = new ArrayList<>();
            List<ZjStoreBoundLog03> log03s = new ArrayList<>();
            List<ZjStoreBoundLog04> log04s = new ArrayList<>();
            List<ZjStoreBoundLog05> log05s = new ArrayList<>();
            for (StoreBoundVo boundVo : storeBoundVos) {
                switch (CodeConstant.tableType(boundVo.getId())) {
                    case 0:
                        logs.add(new ZjStoreBoundLog(boundVo).setType(type));
                        continue;
                    case 1:
                        log02s.add(new ZjStoreBoundLog02(boundVo).setType(type));
                        continue;
                    case 2:
                        log03s.add(new ZjStoreBoundLog03(boundVo).setType(type));
                        continue;
                    case 3:
                        log04s.add(new ZjStoreBoundLog04(boundVo).setType(type));
                        continue;
                    case 4:
                        log05s.add(new ZjStoreBoundLog05(boundVo).setType(type));
                }
            }
            try {
                if (CollectionUtil.isNotEmpty(logs)) this.saveBatch(logs);
                if (CollectionUtil.isNotEmpty(log02s)) storeBoundLog02Service.saveBatch(log02s);
                if (CollectionUtil.isNotEmpty(log03s)) storeBoundLog03Service.saveBatch(log03s);
                if (CollectionUtil.isNotEmpty(log04s)) storeBoundLog04Service.saveBatch(log04s);
                if (CollectionUtil.isNotEmpty(log05s)) storeBoundLog05Service.saveBatch(log05s);
                platformTransactionManager.commit(status);
            }catch (Exception e) {
                logger.error("添加出入库日志错误！原因：" + e);
                platformTransactionManager.rollback(status);
            }
        });
    }
}
